const { FailModel } = require("../utils/model_adapter");
const { privateKey, dePrivatecrypt } = require("../utils/rsa");

// 用于web公众号检查登录
async function checkWxLogin(ctx, next) {
  const token = ctx.headers["x-token"];

  if (!token) {
    return (ctx.body = new FailModel("非法请求", { code: 401 }));
  }
  try {
    const info = dePrivatecrypt(token, privateKey);

    if (!info.txt || !info.kk || !info.card_program_type || !info.ip_address) {
      return (ctx.body = new FailModel("非法请求", { code: 401 }));
    }
    return await next();
  } catch (error) {
    ctx.body = new FailModel("非法请求", { code: 401 });
  }
}

module.exports = checkWxLogin;
